$(function() {
  $(':button#prodscan').bind('click', function() {
    $.getJSON('/_scanflow', {
      ssn: $('input[name="ssn"]').val(),
      wono: $("#wono").text(),
      station: $("#station").text(),
      line: $("#line").text(),
      failcode: $('select[name="failcode"]').val(),
      date: new Date().getTime()+Math.random()
    }, function(data) {
      if(data.result == 'RELOGIN')
      {
        location.href = "/login";
      }
      else
      {
        var beginstr = data.result.substring(0, 4);
        if(beginstr == 'SSN:')
        {
          $("#result").text("");
          $("#result2").text(data.result);
        }
        else
        {
          $("#result").text(data.result);
          $("#result2").text("");
        }
        if($("#result").text() == "")
        {
          var d = new Date().format("yyyy-MM-dd hh:mm:ss");
          var s_ssn = ssn.value;
          var s_sta = $("#station").text();
          $("#flowdetail").val(d + "   " + s_ssn + " finished in station: " + s_sta + "\r" + $("#flowdetail").text());  
        }
        var pqty = data.passedqty;
        if (pqty != "")
        {
          document.getElementById("passqty").innerText = String(pqty);
        }
        var printstr = data.printstr;
        var urltype = data.urltype;
        var urllink = "/queryssn?" + urltype + "=" + printstr;
        if (printstr != "")
        {
          var newwin = window.open(urllink, "newwin", "height=600,width=800,toolbar=no,scrollbars=yes,menubar=no");
          newwin.document.onreadystatechange = function(){   
            if(newwin.document.readyState=="complete")
            {
              newwin.document.getElementById("print").click();
            }
          }
        }
        ssn.value = "";
      }
    });
    return false;
  });
})

function txtKeyDown()
{
  if(event.keyCode == 13)
  {
    prodscan.click();
    failcode.value = "";
    ssn.focus();
  }
}
